﻿<%@ Control Language="VB" AutoEventWireup="false" CodeFile="Uc_staff_fv.ascx.vb" Inherits="Apps_Shares_Uc_staff_fv" %>
<asp:FormView ID="fv" runat="server" DataSourceID="srvFv" Width="100%">
    <InsertItemTemplate>
        <table cellspacing="2" cellpadding="2" bordercolor="#cccccc" border="1" align="center" width="60%">
            <tbody>
                <tr>
                    <td bgcolor="#999999" align="center" class="th_data" colspan="2">
                        &nbsp;
                    </td>
                </tr>
                <tr>
                    <td align="right" class="td_box">
                        เลขประจำตัวพนักงาน <span class="star">*</span> :
                    </td>
                    <td align="left" class="td_box">
                        <asp:TextBox ID="txtStaffNo" runat="server" CssClass="text_normal" Text='<%# Bind("staff_no") %>'
                            MaxLength="5"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td align="right" class="td_box">
                        คำนำหน้า :
                    </td>
                    <td align="left" class="td_box">
                        <asp:DropDownList ID='ddlGender' runat='server' AppendDataBoundItems="True" SelectedValue='<%# Bind("staff_title") %>'>
                            <asp:ListItem Text='คำนำหน้า' Value='0'></asp:ListItem>
                            <asp:ListItem Text='นาย' Value='1'></asp:ListItem>
                            <asp:ListItem Text='นาง' Value='2'></asp:ListItem>
                            <asp:ListItem Text='นางสาว' Value='3'></asp:ListItem>
                            <asp:ListItem Text='คุณ' Value='4'></asp:ListItem>
                        </asp:DropDownList>
                    </td>
                </tr>
                <tr>
                    <td align="right" class="td_box">
                        ชื่อ <span class="star">*</span> :
                    </td>
                    <td align="left" class="td_box">
                        <asp:TextBox ID='txtFirstname' runat='server' CssClass='text_normal' Text='<%# Bind("staff_firstname") %>'></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td align="right" class="td_box">
                        นามสกุล <span class="star">*</span> : </span>
                    </td>
                    <td align="left" class="td_box">
                        <asp:TextBox ID='txtLastname' runat='server' CssClass='text_normal' Text='<%# Bind("staff_lastname") %>'></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td align="right" class="td_box">
                        หน่วยงานที่สังกัด <span class="star">*</span> :
                    </td>
                    <td align="left" class="td_box">
                        <asp:DropDownList ID='ddlDepartment' runat='server' AppendDataBoundItems="True" DataTextField="department_name"
                            DataValueField="department_code" AutoPostBack="true" OnSelectedIndexChanged="ddlDepartment_SelectedIndexChanged"
                            DataSourceID="ddlStpDepartmentSrc" SelectedValue='<%# Bind("department_code") %>'>
                            <asp:ListItem Text='หน่วยงาน' Value='0'></asp:ListItem>
                        </asp:DropDownList>
                        <asp:HiddenField ID='hideDepartment_code' runat='server' Value='<%# Bind("department_code") %>' />
                        <asp:ObjectDataSource ID="ddlStpDepartmentSrc" runat="server" SelectMethod="GetStpDepartmentByPrivilege"
                            TypeName="CodeMatrix.Business.GenericData"></asp:ObjectDataSource>
                    </td>
                </tr>
                <tr>
                    <td align="right" class="td_box">
                        กลุ่มผู้ใช้งาน <span class="star">*</span> :
                    </td>
                    <td align="left" class="td_box">
                        <asp:DropDownList ID='ddlUserGroup' runat='server' DataTextField="group_user_name"
                            DataValueField="group_user_code" DataSourceID="ddlUserGroupDs">
                            <asp:ListItem Text='กลุ่มผู้ใช้งาน ' Value='0'></asp:ListItem>
                        </asp:DropDownList>
                        <asp:HiddenField ID='hidUserGroup' runat='server' Value='<%# Bind("group_user_code") %>' />
                        <asp:SqlDataSource ID="ddlUserGroupDs" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"
                            SelectCommand="select group_user_code = 0 , group_user_name = 'กลุ่มผู้ใช้งาน' 
                            union all select group_user_code , group_user_name from group_user 
                            where department_code = @department_code and group_user_status = 1">
                            <SelectParameters>
                                <asp:ControlParameter Name="department_code" ControlID="ddlDepartment" PropertyName="SelectedValue" />
                            </SelectParameters>
                        </asp:SqlDataSource>
                    </td>
                </tr>
                <tr>
                    <td align="right" class="td_box">&nbsp;</td>
                    <td align="left" class="td_box"><asp:CheckBox ID='cbChangeNewPassword' runat='server' Checked='True'  />เปลี่ยนรหัสผ่านใหม่หลังจาก Login</td>
                </tr>
                <tr>
                    <td align="right" class="td_box">
                        สถานะ <span class="star">*</span> :
                    </td>
                    <td align="left" class="td_box">
                        <asp:RadioButtonList ID='rblStatus' runat='server' RepeatDirection="Horizontal" RepeatLayout='Flow'
                            SelectedValue='<%# Bind("staff_status") %>'>
                            <asp:ListItem Text='ใช้งาน' Value='1' Selected='True'></asp:ListItem>
                            <asp:ListItem Text='ไม่ใช้งาน' Value='2'></asp:ListItem>
                        </asp:RadioButtonList>
                    </td>
                </tr>
                <tr class='hide'>
                    <td align="right" class="td_box">
                        วันที่ลงทะเบียน :
                    </td>
                    <td align="left" class="td_box">
                        <%--12 มีนาคม 2553 --%>
                        <asp:Literal ID='ltrRegisterDate' runat='server' Text='<%# Eval("staff_register_date") %>'></asp:Literal>
                    </td>
                </tr>
                <tr class='hide'>
                    <td align="right" class="td_box">
                        วันที่หมดอายุการใช้งานรหัสผ่าน :
                    </td>
                    <td align="left" class="td_box">
                        <%--12 เมษายน 2553 --%>
                        <asp:Literal ID='ltrExpire' runat='server'></asp:Literal>
                    </td>
                </tr>
                <tr>
                    <td bgcolor="#999999" align="center" class="td_data_end" colspan="2">
                        <asp:Button ID='btnSubmit' runat='server' CommandName='Insert' Text='บันทึก' ValidationGroup='g1' />
                        <asp:Button ID='btnMainMenu' Text='<%$ Resources:Resource,BTN_MAINMENU %>' runat='server' PostBackUrl="~/Apps/ViewSystem.aspx?Edit=2" />
                        <div class="hide">
                            <asp:RequiredFieldValidator ID="txtStaffNoRequiredFieldValidator" runat="server"
                                ControlToValidate="txtStaffNo" ErrorMessage="กรุณากรอกเลขประจำตัวพนักงาน" SetFocusOnError="True"
                                ValidationGroup="g1"></asp:RequiredFieldValidator>
                            <br />
                            <asp:RequiredFieldValidator ID="txtFirstnameRequiredFieldValidator" runat="server"
                                ControlToValidate="txtFirstname" ErrorMessage="กรุณากรอกชื่อพนักงาน" SetFocusOnError="True"
                                ValidationGroup="g1"></asp:RequiredFieldValidator>
                            <br />
                            <asp:RequiredFieldValidator ID="txtLastnameRequiredFieldValidator" runat="server"
                                ControlToValidate="txtLastname" ErrorMessage="กรุณากรอกนามสกุลพนักงาน" SetFocusOnError="True"
                                ValidationGroup="g1"></asp:RequiredFieldValidator>
                            <br />
                            <asp:RequiredFieldValidator ID="ddlUserGroupRequiredFieldValidator" runat="server"
                                ControlToValidate="ddlUserGroup" ErrorMessage="กรุณาเลือกกลุ่มผู้ใช้งาน" InitialValue="0"
                                SetFocusOnError="True" ValidationGroup="g1"></asp:RequiredFieldValidator>
                            <br />
                            <asp:RequiredFieldValidator ID="ddlDepartmentRequiredFieldValidator" runat="server"
                                ControlToValidate="ddlDepartment" ErrorMessage="กรุณาเลือกหน่วยงานที่สังกัด"
                                InitialValue="0" SetFocusOnError="True" ValidationGroup="g1"></asp:RequiredFieldValidator>
                            <br />
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ControlToValidate="rblStatus"
                                ErrorMessage="<%$ Resources:Resource, MSG_REQ_STATUS %>" SetFocusOnError="True"
                                ValidationGroup="g1"></asp:RequiredFieldValidator>
                            <br />
                            <asp:CustomValidator ID="txtStaffNo_CustomValidator" runat="server" ControlToValidate="txtStaffNo"
                                ClientValidationFunction="checkStaffNumber" EnableClientScript="true" ErrorMessage="หมายเลขประจำตัวพนักงานนี้มีอยู่ในระบบแล้ว"
                                ValidationGroup="g1"></asp:CustomValidator>
                            <asp:ValidationSummary ID="fvValidationSummary" runat="server" ShowMessageBox="True"
                                ValidationGroup="g1" />
                        </div>
                    </td>
                </tr>
            </tbody>
        </table>
    </InsertItemTemplate>
    <EditItemTemplate>
        <table cellspacing="2" cellpadding="2" bordercolor="#cccccc" border="1" align="center"
            width="60%">
            <tbody>
                <tr>
                    <td bgcolor="#999999" align="center" class="th_data" colspan="2">
                        &nbsp;
                    </td>
                </tr>
                <tr>
                    <td align="right" class="td_box">
                        เลขประจำตัวพนักงาน <span class="star">*</span> :
                    </td>
                    <td align="left" class="td_box">
                        <asp:TextBox ID="txtStaffNo" runat="server" CssClass="text_normal" Text='<%# Bind("staff_no") %>'
                            MaxLength="5"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td align="right" class="td_box">
                        คำนำหน้า :
                    </td>
                    <td align="left" class="td_box">
                        <asp:DropDownList ID='ddlGender' runat='server' AppendDataBoundItems="True" SelectedValue='<%# Bind("staff_title") %>'>
                            <asp:ListItem Text='คำนำหน้า' Value='0'></asp:ListItem>
                            <asp:ListItem Text='นาย' Value='1'></asp:ListItem>
                            <asp:ListItem Text='นาง' Value='2'></asp:ListItem>
                            <asp:ListItem Text='นางสาว' Value='3'></asp:ListItem>
                            <asp:ListItem Text='คุณ' Value='4'></asp:ListItem>
                        </asp:DropDownList>
                    </td>
                </tr>
                <tr>
                    <td align="right" class="td_box">
                        ชื่อ <span class="star">*</span> :
                    </td>
                    <td align="left" class="td_box">
                        <asp:TextBox ID='txtFirstname' runat='server' CssClass='text_normal' Text='<%# Bind("staff_firstname") %>'></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td align="right" class="td_box">
                        นามสกุล <span class="star">*</span> : </span>
                    </td>
                    <td align="left" class="td_box">
                        <asp:TextBox ID='txtLastname' runat='server' CssClass='text_normal' Text='<%# Bind("staff_lastname") %>'></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td align="right" class="td_box">
                        หน่วยงานที่สังกัด <span class="star">*</span> :
                    </td>
                    <td align="left" class="td_box">
                        <asp:DropDownList ID='ddlDepartment' runat='server' AppendDataBoundItems="True" DataTextField="department_name"
                            DataValueField="department_code" AutoPostBack="true" OnSelectedIndexChanged="ddlDepartment_SelectedIndexChanged"
                            DataSourceID="ddlStpDepartmentSrc" SelectedValue='<%# Bind("department_code") %>'>
                            <asp:ListItem Text='หน่วยงาน' Value='0'></asp:ListItem>
                        </asp:DropDownList>
                        <asp:ObjectDataSource ID="ddlStpDepartmentSrc" runat="server" SelectMethod="GetStpDepartmentByPrivilege"
                            TypeName="CodeMatrix.Business.GenericData"></asp:ObjectDataSource>
                    </td>
                </tr>
                <tr>
                    <td align="right" class="td_box">
                        กลุ่มผู้ใช้งาน <span class="star">*</span> :
                    </td>
                    <td align="left" class="td_box">
                        <asp:DropDownList ID='ddlUserGroup' runat='server' DataTextField="group_user_name"
                            DataValueField="group_user_code" DataSourceID="ddlUserGroupDs">
                            <asp:ListItem Text='กลุ่มผู้ใช้งาน ' Value='0'></asp:ListItem>
                        </asp:DropDownList>
                        <asp:HiddenField ID='hidUserGroup' runat='server' Value='<%# Bind("group_user_code") %>' />
                        <asp:SqlDataSource ID="ddlUserGroupDs" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"
                            SelectCommand="select group_user_code = 0 , group_user_name = 'กลุ่มผู้ใช้งาน' 
                            union all select group_user_code , group_user_name from group_user 
                            where department_code = @department_code and group_user_status = 1">
                            <SelectParameters>
                                <asp:ControlParameter Name="department_code" ControlID="ddlDepartment" PropertyName="SelectedValue" />
                            </SelectParameters>
                        </asp:SqlDataSource>
                    </td>
                </tr>
                <tr>
                    <td align="right" class="td_box">&nbsp;</td>
                    <td align="left" class="td_box">[ <a href="#" onclick="ChangePassword(<%# Eval("staff_code") %>);"> เปลี่ยนรหัสผ่าน</a> ]</td>
                </tr>
                <tr>
                    <td align="right" class="td_box">&nbsp;</td>
                    <td align="left" class="td_box">[ <a href="#" onclick="ResetPassword(<%# Eval("staff_code")%>);">เปลี่ยนรหัสผ่านเป็นค่าเริ่มต้น</a> ]</td>
                </tr>
                <tr>
                    <td align="right" class="td_box">&nbsp;</td>
                    <td align="left" class="td_box"><asp:CheckBox ID='cbChangeNewPassword' runat='server' Checked='<%# Bind("staff_status_login") %>' />เปลี่ยนรหัสผ่านใหม่หลังจาก Login</td>
                </tr>
                <tr>
                    <td align="right" class="td_box">
                        สถานะ <span class="star">*</span> :
                    </td>
                    <td align="left" class="td_box">
                        <asp:RadioButtonList ID='rblStatus' runat='server' RepeatDirection="Horizontal" RepeatLayout='Flow'
                            SelectedValue='<%# Bind("staff_status") %>'>
                            <asp:ListItem Text='ใช้งาน' Value='1' Selected='True'></asp:ListItem>
                            <asp:ListItem Text='ไม่ใช้งาน' Value='2'></asp:ListItem>
                        </asp:RadioButtonList>
                    </td>
                </tr>
                <tr>
                    <td align="right" class="td_box">
                        วันที่ลงทะเบียน :
                    </td>
                    <td align="left" class="td_box">
                        &nbsp;
                        <asp:Literal ID='ltrRegisterDate' runat='server' Text='<%# Eval("staff_register_date") %>'></asp:Literal>
                    </td>
                </tr>
                <tr>
                    <td align="right" class="td_box">
                        วันที่หมดอายุการใช้งานรหัสผ่าน :
                    </td>
                    <td align="left" class="td_box">
                        &nbsp;
                        <asp:Literal ID='ltrExpire' runat='server' Text='<%# Eval("next_expired_date") %>'></asp:Literal>
                    </td>
                </tr>
                <tr>
                    <td align="right" class="td_box">
                        ชื่อและวันเวลาที่สร้างข้อมูล :
                    </td>
                    <td align="left" class="td_box">
                        &nbsp;
                        <asp:Literal ID='ltrCreate' runat='server' Text='<%# Eval("Creator") %>'></asp:Literal>
                    </td>
                </tr>
                <tr>
                    <td align="right" class="td_box">
                        ชื่อและวันเวลาที่ปรับปรุงข้อมูล :
                    </td>
                    <td align="left" class="td_box">
                        &nbsp;
                        <asp:Literal ID='ltrUpdate' runat='server' Text='<%# Eval("Editor") %>'></asp:Literal>
                    </td>
                </tr>
                <tr>
                    <td bgcolor="#999999" align="center" class="td_data_end" colspan="2">
                        <asp:Button ID='btnSubmit' runat='server' CommandName='Update' Text='บันทึก' ValidationGroup="g1" />
                        <asp:Button ID='btnCancel' runat='server' Text='ยกเลิก' PostBackUrl="~/Apps/staff_list.aspx?Edit=true" />
                        <asp:Button ID='btnMainMenu' Text='<%$ Resources:Resource,BTN_MAINMENU %>' runat='server' PostBackUrl="~/Apps/ViewSystem.aspx?Edit=2" />
                        <div class="hide">
                            <asp:RequiredFieldValidator ID="txtStaffNoRequiredFieldValidator" runat="server"
                                ControlToValidate="txtStaffNo" ErrorMessage="กรุณากรอกเลขประจำตัวพนักงาน" SetFocusOnError="True"
                                ValidationGroup="g1"></asp:RequiredFieldValidator>
                            <br />
                            <asp:RequiredFieldValidator ID="txtFirstnameRequiredFieldValidator" runat="server"
                                ControlToValidate="txtFirstname" ErrorMessage="กรุณากรอกชื่อพนักงาน" SetFocusOnError="True"
                                ValidationGroup="g1"></asp:RequiredFieldValidator>
                            <br />
                            <asp:RequiredFieldValidator ID="txtLastnameRequiredFieldValidator" runat="server"
                                ControlToValidate="txtLastname" ErrorMessage="กรุณากรอกนามสกุลพนักงาน" SetFocusOnError="True"
                                ValidationGroup="g1"></asp:RequiredFieldValidator>
                            <br />
                            <asp:RequiredFieldValidator ID="ddlUserGroupRequiredFieldValidator" runat="server"
                                ControlToValidate="ddlUserGroup" ErrorMessage="กรุณาเลือกกลุ่มผู้ใช้งาน" InitialValue="0"
                                SetFocusOnError="True" ValidationGroup="g1"></asp:RequiredFieldValidator>
                            <br />
                            <asp:RequiredFieldValidator ID="ddlDepartmentRequiredFieldValidator" runat="server"
                                ControlToValidate="ddlDepartment" ErrorMessage="กรุณาเลือกหน่วยงานที่สังกัด"
                                InitialValue="0" SetFocusOnError="True" ValidationGroup="g1"></asp:RequiredFieldValidator>
                            <br />
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ControlToValidate="rblStatus"
                                ErrorMessage="<%$ Resources:Resource, MSG_REQ_STATUS %>" SetFocusOnError="True"
                                ValidationGroup="g1"></asp:RequiredFieldValidator>
                            <br />
                            <asp:ValidationSummary ID="fvValidationSummary" runat="server" ShowMessageBox="True"
                                ValidationGroup="g1" />
                        </div>
                    </td>
                </tr>
            </tbody>
        </table>
    </EditItemTemplate>
    <ItemTemplate>
        <table cellspacing="2" cellpadding="2" bordercolor="#cccccc" border="1" align="center"
            width="60%">
            <tbody>
                <tr>
                    <td bgcolor="#999999" align="center" class="th_data" colspan="2">
                        &nbsp;
                    </td>
                </tr>
                <tr>
                    <td align="right" class="td_box">
                        เลขประจำตัวพนักงาน :
                    </td>
                    <td align="left" class="td_box">
                        &nbsp;
                        <asp:Literal ID="txtStaffNo" runat="server" Text='<%# Eval("staff_no") %>'></asp:Literal>
                    </td>
                </tr>
                <tr>
                    <td align="right" class="td_box">
                        คำนำหน้า :
                    </td>
                    <td align="left" class="td_box">
                        &nbsp;
                        <asp:Literal ID='ddlGender' runat='server' Text='<%# CodeMatrix.Business.GenericData.GetPrefixLabel(Eval("staff_title")) %>'></asp:Literal>
                    </td>
                </tr>
                <tr>
                    <td align="right" class="td_box">
                        ชื่อ :
                    </td>
                    <td align="left" class="td_box">
                        &nbsp;
                        <asp:Literal ID='txtFirstname' runat='server' Text='<%# Bind("staff_firstname") %>'></asp:Literal>
                    </td>
                </tr>
                <tr>
                    <td align="right" class="td_box">
                        นามสกุล :
                    </td>
                    <td align="left" class="td_box">
                        &nbsp;
                        <asp:Literal ID='txtLastname' runat='server' Text='<%# Bind("staff_lastname") %>'></asp:Literal>
                    </td>
                </tr>
                <tr>
                    <td align="right" class="td_box">
                        กลุ่มผู้ใช้งาน :
                    </td>
                    <td align="left" class="td_box">
                        &nbsp;
                        <asp:Literal ID='ddlUserGroup' runat='server' Text='<%#CodeMatrix.Business.GenericData.GetGroupUserLabel(me,Eval("group_user_code")) %>'></asp:Literal>
                    </td>
                </tr>
                <tr>
                    <td align="right" class="td_box">
                        หน่วยงานที่สังกัด :
                    </td>
                    <td align="left" class="td_box">
                        &nbsp;
                        <asp:Literal ID='ltrDepartmentCode' runat='server' Text='<%#CodeMatrix.Business.GenericData.GetDepartmentLabel(me,Eval("department_code")) %>'></asp:Literal>
                    </td>
                </tr>
                <tr>
                    <td align="right" class="td_box">
                        เปลี่ยนรหัสผ่านใหม่หลังจาก Login :
                    </td>
                    <td align="left" class="td_box">
                        &nbsp;
                        <%#IIf(CBool(Eval("staff_status_login")), "ใช่", "ไม่")%>
                    </td>
                </tr>
                <tr>
                    <td align="right" class="td_box">
                        สถานะ :
                    </td>
                    <td align="left" class="td_box">
                        &nbsp;
                        <asp:Literal ID='rblStatus' runat='server' Text='<%# CodeMatrix.Business.GenericData.GetStatusLabel(Eval("staff_status")) %>'></asp:Literal>
                    </td>
                </tr>
                <tr>
                    <td align="right" class="td_box">
                        วันที่ลงทะเบียน :
                    </td>
                    <td align="left" class="td_box">
                        &nbsp;
                        <asp:Literal ID='ltrRegisterDate' runat='server' Text='<%# Eval("staff_register_date") %>'></asp:Literal>
                    </td>
                </tr>
                <tr>
                    <td align="right" class="td_box">
                        วันที่หมดอายุการใช้งานรหัสผ่าน :
                    </td>
                    <td align="left" class="td_box">
                        &nbsp;
                        <asp:Literal ID='ltrExpire' runat='server' Text='<%# Eval("next_expired_date") %>'></asp:Literal>
                    </td>
                </tr>
                <tr>
                    <td align="right" class="td_box">
                        ชื่อและวันเวลาที่สร้างข้อมูล :
                    </td>
                    <td align="left" class="td_box">
                        &nbsp;
                        <asp:Literal ID='ltrCreate' runat='server' Text='<%# Eval("Creator") %>'></asp:Literal>
                    </td>
                </tr>
                <tr>
                    <td align="right" class="td_box">
                        ชื่อและวันเวลาที่ปรับปรุงข้อมูล :
                    </td>
                    <td align="left" class="td_box">
                        &nbsp;
                        <asp:Literal ID='ltrUpdate' runat='server' Text='<%# Eval("Editor") %>'></asp:Literal>
                    </td>
                </tr>
                <tr>
                    <td bgcolor="#999999" align="center" class="td_data_end" colspan="2">
                        <asp:Button ID='btnSubmit' runat='server' Text='ย้อนกลับ' PostBackUrl='~/Apps/staff_list.aspx' />
                        <asp:Button ID='btnMainMenu' Text='<%$ Resources:Resource,BTN_MAINMENU %>' runat='server' PostBackUrl="~/Apps/ViewSystem.aspx?Edit=2" />
                    </td>
                </tr>
            </tbody>
        </table>
    </ItemTemplate>
</asp:FormView>
<asp:SqlDataSource ID="srvFv" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"
    InsertCommand="INSERT INTO [staff]
           ([staff_no]
           ,[staff_title]
           ,[staff_firstname]
           ,[staff_lastname]
           ,[group_user_code]
           ,[department_code]
           ,[staff_password]
           ,[staff_password_salt]
           ,[staff_fail_login]
           ,[staff_status_login]
           ,[staff_register_date]
           ,[staff_status]
           ,[staff_code_create]
           ,[create_datetime]
           ,[staff_code_update]
           ,[update_datetime])
     VALUES
           (@staff_no
           ,@staff_title
           ,@staff_firstname
           ,@staff_lastname
           ,@group_user_code
           ,@department_code
           ,@staff_password
           ,@staff_password_salt
           ,@staff_fail_login
           ,@staff_status_login
           ,getdate()
           ,@staff_status
           ,@staff_code_create
           ,getdate()
           ,@staff_code_update
           ,getdate())" 
SelectCommand="select  staff_status = isnull(s.staff_status,0) , s.* ,
		Creator = dbo.UFN_GetAuditor(s.staff_code_create,s.create_datetime) ,
		Editor = dbo.UFN_GetAuditor(s.staff_code_update,s.update_datetime),
		g.group_user_password_expire,
		p.last_change_password,
		CASE WHEN p.last_change_password IS NULL THEN (s.staff_register_date + g.group_user_password_expire) ELSE (p.last_change_password + g.group_user_password_expire) END next_expired_date
from	staff as s
join    group_user as g on s.group_user_code = g.group_user_code
left join	(select MAX(create_datetime) last_change_password, staff_code 
		from log_password where staff_code = @staff_code group by staff_code) 
		p on s.staff_code = p.staff_code 
where @staff_code =  s.staff_code"
UpdateCommand="exec   USP_Update_staff   @staff_title , 
                        @staff_firstname ,
                        @staff_lastname ,
                        @group_user_code ,
                        @department_code ,
                        @staff_status_login ,
                        @staff_code_update ,
                        @staff_status ,
                        @staff_code ,
                        @staff_no">
    <InsertParameters>
        <asp:Parameter Name="staff_no" />
        <asp:Parameter Name="staff_title" />
        <asp:Parameter Name="staff_firstname" />
        <asp:Parameter Name="staff_lastname" />
        <asp:Parameter Name="group_user_code" />
        <asp:Parameter Name="department_code" />
        <asp:Parameter Name="staff_password" />
        <asp:Parameter Name="staff_password_salt" />
        <asp:Parameter Name="staff_fail_login" DefaultValue="0" />
        <asp:Parameter Name="staff_status_login" DefaultValue="1" />
        <asp:Parameter Name="staff_status" />
        <asp:SessionParameter Name="staff_code_create" SessionField="staff_code" />
        <asp:SessionParameter Name="staff_code_update" SessionField="staff_code" />
    </InsertParameters>
    <SelectParameters>
        <asp:QueryStringParameter Name="staff_code" QueryStringField="Edit" />
    </SelectParameters>
    <UpdateParameters>
        <asp:Parameter Name="staff_no" />
        <asp:Parameter Name="staff_title" />
        <asp:Parameter Name="staff_firstname" />
        <asp:Parameter Name="staff_lastname" />
        <asp:Parameter Name="group_user_code" />
        <asp:Parameter Name="department_code" />
        <asp:Parameter Name="staff_status_login" />
        <asp:SessionParameter Name="staff_code_update" SessionField="staff_code" />
        <asp:QueryStringParameter Name="staff_code" QueryStringField="Edit" />
        <asp:Parameter Name="staff_status" />
    </UpdateParameters>
</asp:SqlDataSource>

<script src="../Scripts/json2.js" type="text/javascript"></script>

<script type="text/javascript">
    $(document).ready(function() {

    });

    function checkStaffNumber(oSrc, args) {
        var id = '#' + '<%=txtStaffNoClientID %>';
        var jData = new Object();
        jData.staff_no = $(id).val();

        $.ajax({
            type: "POST",
            cache: false,
            async: false,
            data: JSON.stringify(jData, function(key, value) { return value; }),
            url: "../WebService.asmx/checkDuplicateStaffNo",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            error: function(XMLHttpRequest, textStatus, errorThrown) { alert("Server error \r\n" + XMLHttpRequest.responseText); },
            success: function(response) {
                var result = eval('(' + response.d + ')');
                if (result.Result == 'false')
                    args.IsValid = false;
                else
                    args.IsValid = true;
            }
        });
    }
    function ResetPassword(staff_code) {
        var jData = new Object();
        jData.staff_code = staff_code;

        $.ajax({
            type: "POST",
            cache: false,
            async: false,
            data: JSON.stringify(jData, function(key, value) { return value; }),
            url: "../WebService.asmx/ResetPassword",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            error: function(XMLHttpRequest, textStatus, errorThrown) { alert("Server error \r\n" + XMLHttpRequest.responseText); },
            success: function(response) {
                var result = eval('(' + response.d + ')');
                if (result.Result == 'true')
                    alert('เปลี่ยนรหัสผ่านเรียบร้อยแล้ว');
                else
                    alert('ไม่สามารถเปลี่ยนรหัสผ่านได้');
            }
        });
        return false;
    }
    function ChangePassword(staff_code) {
        var windowName = 'เปลี่ยนพาสเวิร์ด';
        win = window.open('ChangePassword.aspx?scid=' + staff_code + '&pop=1', windowName, 'width = 500px, height = 300px, scrollbars = no, resizable = 0, menubar = 0')
        win.moveTo(100, 100);
        return false;
    }
</script>